<template>
	<div class="box-container">
		<div id="md-content" class="article-content"></div>
	</div>
</template>

<script setup>
import { onMounted } from 'vue';
import { useRoute } from 'vue-router';
import md from '../module/markdownit/markdown-it.js';
import { buildCodeBlock, deepCloneAndUpdate } from '../module/markdownit/code-block.js';
import { getArticleContent } from '@/api/portal.js';

const route = useRoute();

const onRenderContent = function(articleId) {
	getArticleContent(articleId).then(resp => {
		if (resp.code === 200) {
			renderMarkdown(resp.data);
			return;
		}
	})
}

const renderMarkdown = function(data) {
	const el = document.getElementById('md-content');
	if (!el) {
		console.error('Element not found');
		return;
	};

	const tmpDiv = document.createElement('div');
	tmpDiv.innerHTML = md.render(data);
	buildCodeBlock(tmpDiv);

	deepCloneAndUpdate(el, tmpDiv);
}

onMounted(() => {
	const articleId = route.params.articleId;
	onRenderContent(articleId);
})
</script>

<style scoped>
.article-content {
	width: 100%;
	height: 100%;
	padding: 20px;
}

.article-content table {
	@apply w-full border-collapse;

	th, td {
		@apply p-2 border border-gray-200;
	}
}

.article-content pre, .comment-item-content pre {
	position: relative;
}

.copy-btn {
	position: absolute;
	top: 0;
	right: 0;
	border-radius: 3px;
	padding: 3px 10px;
	font-size: 12px;
	font-family: Lato, PingFang SC, Microsoft YaHei, sans-serif;
	font-weight: normal;
	background: #fff;
	color: #000;
	cursor: pointer;
	opacity: 0;
	margin: 8px 10px;
	transition: all 0.3s;
	z-index: 5;
}

.copy-btn:hover {
	background-color: #ddd;
}
</style>
